#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alut.h>
#include "pch.h"

int main()
{
    ALCdevice *device;
    ALCcontext *context;
    ALuint source, buffer;
    ALsizei size, freq;
    ALenum format;
    void *data;
    bool loop;

    // Initialize OpenAL
    device = alcOpenDevice(NULL);
    if (!device)
    {
        fprintf(stderr, "Failed to open audio device\n");
        return 1;
    }
    context = alcCreateContext(device, NULL);
    alcMakeContextCurrent(context);

    // Generate a source and buffer
    alGenSources(1, &source);
    alGenBuffers(1, &buffer);

    // Load the WAV file
    signed char path[] = "powerup.wav";

    struct Audio
    {
        ALenum format;
        void *data;
        ALsizei size;
        ALsizei freq;
        ALboolean loop;
        
        void Load(signed char *path)
        {
            alutLoadWAVFile(path, &format, &data, &size, &freq, &loop);
        }
    };

    alutLoadWAVFile(path, &format, &data, &size, &freq, NULL);

    // Fill the buffer with audio data
    alBufferData(buffer, format, data, size, freq);

    // Associate the buffer with the source
    alSourcei(source, AL_BUFFER, buffer);

    // Play the sound
    alSourcePlay(source);

    // Wait for the sound to finish
    ALint state;
    do
    {
        alGetSourcei(source, AL_SOURCE_STATE, &state);
    } while (state == AL_PLAYING);

    // Clean up
    alDeleteSources(1, &source);
    alDeleteBuffers(1, &buffer);
    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return 0;
}
